package com.me.interceptor;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;

import com.me.entity.StudentPaper;

@Controller
public class PaperInterceptor implements HandlerInterceptor {
	// 获取日志记录器
	protected final static Logger LOG = LoggerFactory.getLogger(PaperInterceptor.class);
	
	@Override
	public boolean preHandle(HttpServletRequest request,
			HttpServletResponse response, Object handler) throws Exception {
		HttpSession session = request.getSession();
		String servletPath = request.getServletPath();
		
		LOG.info("------------访问Servlet组件：" + servletPath + "------------");
		
/*		if(servletPath.equals("/login/studentIndex.do")){
			if(session.getAttribute("choice") != null || session.getAttribute("question") != null){
				return false;
			}
		}*/
		/*
		if(servletPath.equals("/questionLib/questionList.do")){
			if(session.getAttribute("questionPage") != null ){
				return false;
			} else{
				session.setAttribute("questionPage", "firstLoad");
				return true;
			}
		}*/
		// 判断试卷状态，如果为状态为3-已经交卷，则直接返回到登录页
		StudentPaper studentPaper = (StudentPaper) session.getAttribute("student");
		if(studentPaper != null && studentPaper.getPaperState() == 3){
			response.sendRedirect(request.getContextPath() + "/login/toLogin.do");
			return false;
		}
		return true;
	}

	@Override
	public void postHandle(HttpServletRequest request,
			HttpServletResponse response, Object handler,
			ModelAndView modelAndView) throws Exception {

		
	}

	@Override
	public void afterCompletion(HttpServletRequest request,
			HttpServletResponse response, Object handler, Exception ex)
			throws Exception {
		// TODO Auto-generated method stub
		
	}

}
